preserve
qui: by eventdate, sort: egen natempl = total(employment_sa)
qui: keep eventdate natempl 
qui: duplicates drop
qui: sort eventdate
qui: gen idtime = _n
merge 1:1 idtime using oecd_dates.dta
qui: drop _merge
qui: tsset eventdate
qui: gen breakdate = 0
qui: replace breakdate = 1 if idtime > 60
qui: gen interaction = idtime
qui: replace interaction = interaction - 60
qui: replace interaction = 0 if interaction < 0
qui: gen logemployment = ln(natempl)
qui: tsfilter hp cycle_hp_employment_sa = logemployment, smooth(1600) trend(trend_hp)
qui: reg logemployment idtime 
qui: mat A = e(b)
qui: gen cycle_linear_employment_sa = logemployment - A[1,1]*idtime - A[1,2]
di "Use ${var_dating} for business cycle dating"
qui: sbbq logemployment
qui: ren logemployment_point natempl_point
tsline logemployment, lcolor(black) tline(1991q2 1995q3 1998q1 2001q2 2008q2 2011q3, lcolor(black) lpattern(dash)) tline(1993q3 1996q1 1998q4 2005q1 2009q2 2013q1, lcolor(black) lpattern(dot)) xtitle("Time") ytitle("National employment") graphregion(color(white))
graph export ${graphs}/nationaloecddating${version}.png, replace
tsline logemployment, lcolor(black) tline(1992q1 2001q1 2008q3, lcolor(black) lpattern(dash)) tline(1997q4 2005q2 2009q4, lcolor(black) lpattern(dot)) xtitle("Time") ytitle("National employment") graphregion(color(white)) 
graph export ${graphs}/nationalemploymentdating${version}.png, replace
save nationalemployment.dta, replace
restore
